﻿using AutoMapper;
using AutoMapper.Internal.Mappers;
using Microsoft.EntityFrameworkCore;
using Rbac.Contracts.DTO.InPut;
using Rbac.Contracts.DTO.OutPut;
using Rbac.Contracts.Interface;
using Rbac.Domain.Interface;
using System.Runtime.Serialization;

namespace Rbac.Server
{
	public class UserServer : ServerBase,IUserServer
	{
		private readonly IUserRepository _userRepository;
		private readonly IUserRoleRepository _userRoleRepository;
		private readonly IRoleRepository _roleRepository;
		public UserServer(IUserRepository userRepository, IUserRoleRepository userRoleRepository, IRoleRepository roleRepository)
		{
			_userRepository = userRepository;
			_userRoleRepository = userRoleRepository;
			_roleRepository = roleRepository;
		}
		/// <summary>
		/// 获取查询分页
		/// </summary>
		/// <returns></returns>
		public PageOutPut<UserOutPut> GetUserOuts(GetUsersInput input)
		{
			//拿到所有的用户
			var list = _userRepository.GetUsers().Where(c=>c.IsDeleted==false).AsNoTracking();
			if(!string.IsNullOrWhiteSpace(input.UserName))
			{
				//如果我们在username上面建立了索引，那最好使用startwith
				list=list.Where(c=>c.UserName!.StartsWith(input.UserName));
			}
			if(!string.IsNullOrWhiteSpace(input.Email))
			{
				list = list.Where(c => c.Email!.Contains(input.Email));
			}
			//创建分页对象
			var result = new PageOutPut<UserOutPut>()
			{
				Total = list.Count(),
				PageSize = input.PageSize,
			};
			//分页
			var list1 = list.OrderBy(c=>c.UserId).Skip((input.PageIndex-1)*input.PageSize).Take(input.PageSize).ToList();
			//用户id集合
			var userIdList = list1.Select(c => c.UserId);
			//根据用户id查询角色
			var roleList=(from ur in _userRoleRepository.UserRoles()
			join r in _roleRepository.GetRoles()
			on ur.RoleId equals r.RoleId
			where userIdList.Contains(ur.UserId)
			select new
			{
				ur.UserId,
				r.RoleName,
				r.RoleId
			}).ToList();
			var roleGroup = roleList.GroupBy(c => c.UserId).ToList();
			//var roles=_userRoleRepository.UserRolesByUserId(userIdList.ToArray());
			result.Data = _mapper.Map<List<UserOutPut>>(list1);
			foreach (var item in result.Data)
			{
				foreach (var g in roleGroup)
				{
					if(item.UserId==g.Key)
					{
						item.RoleOutPuts = g.ToList().Select(c => new RoleOutPut
						{
							RoleId = c.RoleId,
							RoleName = c.RoleName
						}).ToList();
					}
				}
			}
			return result;
		}
		/// <summary>
		/// 登录
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public UserOutPut Login(LoginInPut input)
		{
			var user= _userRepository.Login(input.UserName, input.Pwd); 
			var dto= _mapper.Map<UserOutPut>(user);
			dto.RoleOutPuts=_mapper.Map<List<RoleOutPut>>(_userRoleRepository.UserRolesByUserId(dto.UserId));
			return dto;
		}
		/// <summary>
		/// 获取用户信息(包括角色信息)
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		public UserOutPut GetUserByUserId(int userId)
		{
			var user=_userRepository.GetUsers().AsNoTracking()
				.FirstOrDefault(c=>c.UserId==userId);
			var userDto=_mapper.Map<UserOutPut>(user);
			//获取角色信息
			userDto.RoleOutPuts=_mapper.Map<List<RoleOutPut>>(_userRoleRepository.UserRolesByUserId(userId));
			return userDto;
		}
	}
}